<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xsl:stylesheet [
  <!ENTITY nbsp "&#x00A0;">
]>
<xsl:stylesheet
  version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:msxml="urn:schemas-microsoft-com:xslt"
  xmlns:site.library="urn:site.library"
  xmlns:umbraco.library="urn:umbraco.library" xmlns:Exslt.ExsltCommon="urn:Exslt.ExsltCommon" xmlns:Exslt.ExsltDatesAndTimes="urn:Exslt.ExsltDatesAndTimes" xmlns:Exslt.ExsltMath="urn:Exslt.ExsltMath" xmlns:Exslt.ExsltRegularExpressions="urn:Exslt.ExsltRegularExpressions" xmlns:Exslt.ExsltStrings="urn:Exslt.ExsltStrings" xmlns:Exslt.ExsltSets="urn:Exslt.ExsltSets"
  exclude-result-prefixes="msxml umbraco.library Exslt.ExsltCommon Exslt.ExsltDatesAndTimes Exslt.ExsltMath Exslt.ExsltRegularExpressions Exslt.ExsltStrings Exslt.ExsltSets site.library">


  <xsl:output method="xml" omit-xml-declaration="yes" />

  <xsl:param name="currentPage"/>
  <xsl:variable name="curSite" select="$currentPage/ancestor-or-self::*[@isDoc and name()='Site']"/>

  <xsl:variable name="macroObjId" select="string(/macro/macroPage)"/>
  <xsl:variable name="macroObj" select="umbraco.library:GetXmlNodeById($macroObjId)"/>
  <xsl:variable name="StartLevel" select="site.library:ShortIf($macroObjId!='' and $macroObj/startLevel,$macroObj/startLevel,2)"/>
  <xsl:variable name="MaxLevel" select="site.library:ShortIf($macroObjId!='' and $macroObj/tillLevel,$macroObj/tillLevel,4)"/>

  <xsl:variable name="selPages" select="$currentPage/ancestor-or-self::*"/>
  <xsl:variable name="selPagesIds" select="$selPages/@id"/>

  <xsl:variable name="isEditMode" select="site.library:IsCanvas()"/>


  <xsl:template match="/">    
      <ul class="left-menu">
        <xsl:call-template name="ProcessChilds">
          <xsl:with-param name="Page" select="$curSite"/>
          <xsl:with-param name="startLevel" select="$StartLevel"/>
          <xsl:with-param name="maxLevel" select="$MaxLevel"/>
        </xsl:call-template>
      </ul>
  </xsl:template>

  <xsl:template name="ProcessChilds">
    <xsl:param name="startLevel"/>
    <xsl:param name="maxLevel"/>
    <xsl:param name="Page"/>
    <xsl:param name="CurLevel" select="1"/>

    <xsl:for-each select="$Page/*[@isDoc and string(hideInMenu) != '1']">
      <xsl:variable name="Child" select="current()"/>
      <xsl:if test="count($selPagesIds[.=$Child/@id])>0 or ($CurLevel = $startLevel)">
        <xsl:choose>
          <xsl:when test="$CurLevel &lt; $startLevel">
            <xsl:call-template name="ProcessChilds">
              <xsl:with-param name="Page" select="$Child"/>
              <xsl:with-param name="CurLevel" select="$CurLevel+1"/>
              <xsl:with-param name="startLevel" select="$startLevel"/>
              <xsl:with-param name="maxLevel" select="$maxLevel"/>
            </xsl:call-template>
          </xsl:when>
          <xsl:when test="$CurLevel = $startLevel">
            <xsl:call-template name="RenderItem">
              <xsl:with-param name="Page" select="$Child"/>
              <xsl:with-param name="CurLevel" select="$CurLevel"/>
              <xsl:with-param name="exClass" select="'startel'"/>
              <xsl:with-param name="maxLevel" select="$maxLevel"/>
            </xsl:call-template>
          </xsl:when>
        </xsl:choose>
      </xsl:if>
    </xsl:for-each>
  </xsl:template>

  <xsl:template name="RenderItem">
    <xsl:param name="Page" select="false"/>
    <xsl:param name="exClass"/>
    <xsl:param name="CurLevel" select="1"/>
    <xsl:param name="maxLevel"/>
    <xsl:variable name="isSelected" select="count($selPagesIds[number(.)=number($Page/@id)])>0"/>
    <xsl:variable name="itemUrl" select="site.library:ShortIf($Page/navigateUrl and $Page/navigateUrl != '',$Page/navigateUrl,umbraco.library:NiceUrl($Page/@id))"/>
    <li class="mitem mitem_l{$CurLevel} mi_{position()} {$exClass} {site.library:ShortIf($isSelected,'active','')}" >
      <a href="{site.library:ShortIf($isSelected, 'javascript:void(0)', $itemUrl)}" class="mlink mlink_{$CurLevel}">
        <xsl:if test="$isEditMode"><xsl:attribute name="onclick">javascript:showEditNodeDialog( '<xsl:value-of disable-output-escaping="yes" select="$Page/@id"/>', '<xsl:value-of disable-output-escaping="yes" select="$Page/title"/>');</xsl:attribute></xsl:if>
        <xsl:if test="string($Page/target) != ''"><xsl:attribute name="target"><xsl:value-of select="$Page/target"></xsl:value-of></xsl:attribute></xsl:if>
        <xsl:if test="string($Page/menuImage) != ''"><img alt="{$Page/@nodeName}" src="{umbraco.library:GetMedia($Page/menuImage, 0)/umbracoFile}"/></xsl:if>
        <xsl:if test="not (string($Page/menuImageOnly) = '1')">
          <!--show [title] or [menu title]-->
          <xsl:value-of select="site.library:ShortIf(string-length($Page/menuTitle)>0,$Page/menuTitle,$Page/title)" disable-output-escaping="yes"/>
        </xsl:if>
      </a>
      <xsl:variable name="Childs" select="$Page/*[@isDoc and string(hideInMenu) != '1']"/>
      <xsl:if test="count($Childs)>0 and $CurLevel &lt;= $maxLevel">
        <ul class="leftmenu_{$CurLevel}">
          <xsl:for-each select="$Childs">
            <xsl:variable name="Child" select="current()"/>
            <xsl:variable name="isCISelected" select="count($selPagesIds[number(.)=number($Child/@id)])>0"/>
            <xsl:call-template name="RenderItem">
              <xsl:with-param name="Page" select="$Child"/>
              <xsl:with-param name="CurLevel" select="$CurLevel+1"/>
              <xsl:with-param name="exClass"><xsl:if test ="$isCISelected"> active</xsl:if><xsl:if test="position()=last()"> islast</xsl:if><xsl:if test="position()=1"> isfirst</xsl:if></xsl:with-param>
            </xsl:call-template>
          </xsl:for-each>
        </ul>
      </xsl:if>
    </li>
  </xsl:template>


</xsl:stylesheet>